package com.zx.BookSeaReader.ui.home;


import android.Manifest;
import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.navigation.NavigationView;
import com.google.android.material.tabs.TabLayout;
import com.zx.BookSeaReader.R;

import com.zx.BookSeaReader.common.APPCONST;
import com.zx.BookSeaReader.greendao.entity.UserBean;
import com.zx.BookSeaReader.ui.downloadbook.DownLoadBooksActivity;
import com.zx.BookSeaReader.ui.local.FileSystemActivity;
import com.zx.BookSeaReader.ui.login.LoginActivity;
import com.zx.BookSeaReader.util.PermissionsChecker;
import com.zx.BookSeaReader.util.SystemBarTintManager;
import com.zx.BookSeaReader.util.TextHelper;

import butterknife.BindView;
import butterknife.ButterKnife;


public class MainActivity extends FragmentActivity implements NavigationView.OnNavigationItemSelectedListener{


    @BindView(R.id.tl_tab_menu)
    TabLayout tlTabMenu;
    @BindView(R.id.civ_icon)
    ImageView ivIcon;
    @BindView(R.id.iv_search)
    ImageView ivSearch;

    @BindView(R.id.rl_common_title)
    RelativeLayout rlCommonTitle;
    @BindView(R.id.tv_edit_finish)
    TextView tvEditFinish;
    @BindView(R.id.rl_edit_titile)
    RelativeLayout rlEditTitile;
    @BindView(R.id.vp_content)
    ViewPager vpContent;
    private MainPrensenter mMainPrensenter;
    private DrawerLayout drawer;
    private static final int WAIT_INTERVAL = 2000;
    private static final int PERMISSIONS_REQUEST_STORAGE = 1;
    public UserBean userBean;


    static final String[] PERMISSIONS = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    private PermissionsChecker mPermissionsChecker;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);

        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        userBean = (UserBean)getIntent().getSerializableExtra(APPCONST.USER);
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        ButterKnife.bind(this);
        setStatusBar(R.color.white);

        ivIcon.setOnClickListener(
                (v) -> {
                    drawer.openDrawer(GravityCompat.START);
                }
        );

        mMainPrensenter = new MainPrensenter(this);
        mMainPrensenter.start();

    }
    public UserBean getUser() {
        return userBean;
    }







    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - APPCONST.exitTime > APPCONST.exitConfirmTime) {
            TextHelper.showText("再按一次退出");
            APPCONST.exitTime = System.currentTimeMillis();
        } else {
            finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();

    }




    public TabLayout getTlTabMenu() {
        return tlTabMenu;
    }

    public ImageView getIvSearch() {
        return ivSearch;
    }

    public ViewPager getVpContent() {
        return vpContent;
    }

    public RelativeLayout getRlCommonTitle() {
        return rlCommonTitle;
    }

    public TextView getTvEditFinish() {
        return tvEditFinish;
    }

    public RelativeLayout getRlEditTitile() {
        return rlEditTitile;
    }


    @TargetApi(19)
    protected void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }


    /**
     * 设置状态栏颜色
     * @param colorId
     */
    public void setStatusBar(int colorId){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintResource(colorId);//通知栏所需颜色ID
        }
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        int id = item.getItemId();
        Class<?> activityCls = null;
        switch (id) {
            case R.id.nav_user:
                activityCls = LoginActivity.class;
                break;
            case R.id.nav_download:
                activityCls = DownLoadBooksActivity.class;
                break;

//            case R.id.nav_local:
//
//                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M){
//
//                    if (mPermissionsChecker == null){
//                        mPermissionsChecker = new PermissionsChecker(this);
//                    }
//
//                    //获取读取和写入SD卡的权限
//                    if (mPermissionsChecker.lacksPermissions(PERMISSIONS)){
//                        //请求权限
//                        ActivityCompat.requestPermissions(this, PERMISSIONS,PERMISSIONS_REQUEST_STORAGE);
//                        return super.onOptionsItemSelected(item);
//                    }
//                }
//
//                //activityCls = FileSystemActivity.class;
//                break;

            case R.id.nav_feedback:
                break;

            case R.id.nav_settings:
                break;
            default:
                break;
        }
        if (activityCls != null){
            Intent intent = new Intent(this, activityCls);
            intent.putExtra(APPCONST.USER, userBean);
            startActivity(intent);
        }


        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}
